﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Prism.Collections.Specialized;
using Prism.Collections.Generic;

namespace Prism.Collections.ObjectModel
{
    [Serializable]
    public class ObservableTree<NodeType, ValueType> : Tree<NodeType, ValueType>, INotifyTreeChanged, INotifyPropertyChanged
        where NodeType : TreeNode<ValueType>
    {
        public ObservableTree()
            : base()
        {
            this.treeProxy = new ObservableCollection<NodeType>();
        }

        public override void Add(NodeType parentNode, NodeType childNode, ValueType value)
        {
            var oldNodes = new List<TreeNode<ValueType>>();
            foreach (NodeType i in this)
            {
                oldNodes.Add(i);
            }

            base.Add(parentNode, childNode, value);

            var newNodes = new List<TreeNode<ValueType>>();
            foreach (NodeType i in this)
            {
                newNodes.Add(i);
            }

            this.OnTreeChanged(new NotifyTreeChangedEventArgs() { Action = NotifyTreeChangedAction.Add, OldNodes = oldNodes, NewNodes = newNodes });
        }

        public override void RemoveLeaf(NodeType node)
        {
            var oldNodes = new List<TreeNode<ValueType>>();
            foreach (NodeType i in this)
            {
                oldNodes.Add(i);
            }

            base.RemoveLeaf(node);

            var newNodes = new List<TreeNode<ValueType>>();
            foreach (NodeType i in this)
            {
                newNodes.Add(i);
            }

            this.OnTreeChanged(new NotifyTreeChangedEventArgs() { Action = NotifyTreeChangedAction.RemoveLeaf, OldNodes = oldNodes, NewNodes = newNodes });
        }

        public override void Clear()
        {
            var oldNodes = new List<TreeNode<ValueType>>();
            foreach (NodeType i in this)
            {
                oldNodes.Add(i);
            }

            base.Clear();

            var newNodes = new List<TreeNode<ValueType>>();

            this.OnTreeChanged(new NotifyTreeChangedEventArgs() { Action = NotifyTreeChangedAction.Reset, OldNodes = oldNodes, NewNodes = newNodes });
        }

        public override NodeType TopNode
        {
            get
            {
                return base.TopNode;
            }
            protected set
            {
                base.TopNode = value;
                this.treeProxy.Clear();
                if (this.TopNode != null)
                {
                    this.treeProxy.Add(this.TopNode);
                }
                this.OnPropertyChanged("TopNode");
            }
        }

        private IList<NodeType> treeProxy;
        public virtual IList<NodeType> TreeProxy
        {
            get { return this.treeProxy; }
        }

        #region INotifyTreeChanged Members

        [field: NonSerialized]
        public event NotifyTreeChangedEventHandler TreeChanged;

        protected void OnTreeChanged(NotifyTreeChangedEventArgs e)
        {
            if (this.TreeChanged != null)
            {
                this.TreeChanged(this, e);
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// When a property changes.
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// The PropertyChanged event is raised.
        /// </summary>
        /// <param name="e">The event contains the parameter.</param>
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }

        /// <summary>
        /// The PropertyChanged event is raised.
        /// </summary>
        /// <param name="propertyName">Change the name of the attribute.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
